﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;

namespace XWord.WPF.Behaviors
{
    //When Binding Property don't implement INotifyPropertyChanged
    public class RadioButtonInverseBehavior : Behavior<RadioButton>
    {

        protected override void OnAttached()
        {
            //AssociatedObject.Checked += new RoutedEventHandler(AssociatedObject_Checked);
            //Target.Checked += new RoutedEventHandler(Target_Checked);
      
            base.OnAttached();
        }

        void AssociatedObject_Checked(object sender, RoutedEventArgs e)
        {
            if (DataContext == null) return;
            DataContext.GetType().GetProperty(BindingPropertyName).SetValue(DataContext, true, null);
        }

        void Target_Checked(object sender, RoutedEventArgs e)
        {
            if (DataContext == null) return;
            DataContext.GetType().GetProperty(BindingPropertyName).SetValue(DataContext, false, null);
        }

        public object DataContext
        {
            get { return (object)GetValue(DataContextProperty); }
            set { SetValue(DataContextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DataContext.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataContextProperty =
            DependencyProperty.Register("DataContext", typeof(object), typeof(RadioButtonInverseBehavior), 
            new UIPropertyMetadata(new PropertyChangedCallback((d,arg)=>{
                var behavior = d as RadioButtonInverseBehavior;
                
                if (behavior.DataContext != null)
                {
                    behavior.AssociatedObject.IsChecked = (bool)behavior.DataContext.GetType().
                        GetProperty(behavior.BindingPropertyName).GetValue(behavior.DataContext, null);
                    behavior.aa();
                    //behavior.Target.IsChecked = !behavior.AssociatedObject.IsChecked;
                    //behavior.aa();
                }
            })));

        private void aa()
        {
            var dpd = DependencyPropertyDescriptor.FromProperty(RadioButton.IsCheckedProperty, Target.GetType());
            dpd.RemoveValueChanged(Target, dpdValueChanged);
            dpd.AddValueChanged(Target, dpdValueChanged);
            var dpd1 = DependencyPropertyDescriptor.FromProperty(RadioButton.IsCheckedProperty, AssociatedObject.GetType());
            dpd1.RemoveValueChanged(AssociatedObject, dpd1ValueChanged);
            dpd1.AddValueChanged(AssociatedObject, dpd1ValueChanged);
        }

        private void dpdValueChanged(object sender, EventArgs arg)
        {
            AssociatedObject.IsChecked = !Target.IsChecked;
        }

        private void dpd1ValueChanged(object sender, EventArgs arg)
        {
            Target.IsChecked = !AssociatedObject.IsChecked;
        }

        public string BindingPropertyName { get; set; }


        public RadioButton Target
        {
            get { return (RadioButton)GetValue(TargetProperty); }
            set { SetValue(TargetProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Target.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetProperty =
            DependencyProperty.Register("Target", typeof(RadioButton), typeof(RadioButtonInverseBehavior), 
            new UIPropertyMetadata(null));


    }
}
